package com.ruoyi.network.listener;

import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.event.NetworkEvent;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.network.handler.NetworkEventHandler;

import lombok.extern.slf4j.Slf4j;
@Component
@Slf4j
public class NetworkEventListener implements ApplicationListener<NetworkEvent>{
	@Override
	public void onApplicationEvent(NetworkEvent event) {
		JSONObject message = JSON.parseObject(event.getMessage());
		JSONObject param = message.getJSONObject("param");
		JSONObject command = message.getJSONObject("command");
		int iCommand = command.getIntValue("iCommand");
		String simpleName = NetworkEventHandler.class.getSimpleName();
		String beanKey = simpleName.substring(0, 1).toLowerCase() + simpleName.substring(1)  + iCommand;
		try {
			if(SpringUtils.containsBean(beanKey)) {
				NetworkEventHandler networkEventHandler = SpringUtils.applicationContext.getBean(beanKey, NetworkEventHandler.class);
				networkEventHandler.invoke(message);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}

}
